package controllers

import (
	goerrors "github.com/go-errors/errors"
)

var ErrMap map[string]map[int]string

type CustomError struct {
	errCode int            `json:"errCode"`
	Msg     string         `json:"msg"`
	Data    interface{}    `json:"data"`
	err     goerrors.Error `json:"err"`
}

func (err *CustomError) Error() string {
	return err.err.Error()
}

func (err *CustomError) ErrorStack() string {
	return err.err.ErrorStack()
}

func NewError(errCode int, e interface{}) *CustomError {
	goerrors.MaxStackDepth = 5
	return &CustomError{
		errCode: errCode,
		err:     *goerrors.New(e),
	}
}

func wrap(locale string, e interface{}) *CustomError {

	switch et := e.(type) {
	case *CustomError:
		ce := e.(*CustomError)
		ce.Msg = ErrMap[locale][ce.errCode]
		return ce
	case error:
		return &CustomError{Msg: "unknown err", err: *goerrors.New(et)}
	default:
		return &CustomError{Msg: "success", Data: e}
	}
}
